package com.zz.tijian.handle.pay.res;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zz.tijian.config.AliPayConfig;
import com.zz.tijian.entity.Orders;
import com.zz.tijian.entity.req.PayDto;
import com.zz.tijian.entity.req.PayOrderDto;
import com.zz.tijian.entity.res.OrderPlusVo;
import com.zz.tijian.entity.res.Result;
import com.zz.tijian.expections.BadRequestException;
import com.zz.tijian.expections.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AlipayStrategy implements PayStrategy{
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private AliPayConfig aliPayConfig;
    @Override
    public Result<Object> pay(PayDto payDto) {
        //调用第三方支付
        // 实例化客户端,填入所需参数
        AlipayTradePagePayRequest request = new
                AlipayTradePagePayRequest();
        // 在公共参数中设置回跳和通知地址
        request.setReturnUrl(aliPayConfig.getRETURN_URL());
        //request.setNotifyUrl(aliPayConfig.getNOTIFY_URL());

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = payDto.getOrderId().toString();
        // 付款金额，必填
        // String total_amount = order.getSetmeal().getPrice().toString();

        String total_amount = payDto.getPrice();
        // 订单名称，必填
        String subject = "熙心健康体检";
        // 商品描述，可空
        String body = payDto.getSetmealName();
        request.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            log.error("支付宝调用异常", e);
        }

        return Result.ok(form);
    }
}
